package example.mq;

import example.constant.CommentConstant;
import example.entity.UserEntity;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description: 生产者
 * @Author: mingtian
 * @CreateDate: 2021/1/17 11:15
 * @Version: 1.0
 */
@RestController
public class FanoutProducer {
    @Autowired

    private AmqpTemplate amqpTemplate;

    /**
     * 发送 字符串方式消息
     *
     * @param msg
     * @return
     */
    @RequestMapping("/sendMsgString")
    public String sendMsgString(String msg) {
        // 参数1 交换机名称 、参数2路由key  参数3 消息
        amqpTemplate.convertAndSend(CommentConstant.EXCHANGE_NAME, "", msg);
        return "success";
    }

    /**
     * 发送对象方式消息
     *
     * @return
     */
    @RequestMapping("/sendMsgUserEntity")
    public String sendMsgUserEntity() {
        UserEntity userEntity = new UserEntity("明天", 25, "上海");
        // 参数1 交换机名称 、参数2路由key  参数3 消息
        amqpTemplate.convertAndSend(CommentConstant.EXCHANGE_NAME, "", userEntity);
        return "success";
    }
}
